package com.cskaoyan.service;

import com.cskaoyan.bean.Collect;
import com.cskaoyan.bean.CollectExample;
import com.cskaoyan.bean.wx.vo.CollectVo;
import com.cskaoyan.mapper.CollectMapper;
import com.cskaoyan.utils.DateUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CollectServiceImpl implements CollectService {
    @Autowired
    CollectMapper collectMapper;

    @Override
    public Map<String, Object> list(Integer type, Integer page, Integer size, Integer userId) {

        PageHelper.startPage(page, size);

        List<CollectVo> collectList = collectMapper.selectGoodsByUserId(userId);

        PageInfo pageInfo = new PageInfo(collectList);
        long totalPages = pageInfo.getTotal();

        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("totalPages", totalPages);
        map.put("collectList", collectList);
        return map;
    }

    @Override
    public Boolean addordelete(Collect collect) {
        CollectExample collectExample = new CollectExample();
        collectExample.createCriteria().andValueIdEqualTo(collect.getValueId()).andTypeEqualTo(collect.getType());
        List<Collect> collects = collectMapper.selectByExample(collectExample);
        if (collects == null || collects.size() == 0) {
            collectMapper.insertSelective(collect);
            return true;
        }
        Boolean deleted = collects.get(0).getDeleted();
        // 判断测试该商品对应收藏记录的状态
        if (deleted) {
            collect.setDeleted(false);
            collectMapper.updateByExampleSelective(collect, collectExample);
            return true;
        }

        collect.setDeleted(true);
        collectMapper.updateByExampleSelective(collect, collectExample);
        return false;

    }
}
